package com.triones.api.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysUserImportVo;
import com.ruoyi.system.listener.SysUserImportListener;
import com.triones.api.entity.CountryDictDO;
import com.triones.api.entity.HelpCategorizeDO;
import com.triones.api.entity.param.MemberParams;
import com.triones.api.entity.vo.CountryDictVO;
import com.triones.api.entity.vo.CountryImportVo;
import com.triones.api.entity.vo.CustomMemberVO;
import com.triones.api.entity.vo.HelpCategorizeVO;
import com.triones.api.service.CountryDictService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 国家字典
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/countryDict")
public class CountryDictController extends BaseController {

    private final CountryDictService countryDictService;

    /**
     * 导入数据
     */
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<CountryImportVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), CountryImportVo.class, true);
        List<CountryImportVo> list = excelResult.getList();
        countryDictService.importData(list);
        return R.ok(excelResult.getAnalysis());
    }

    /**
     * 根据层级获取列表数据（分页）
     */
    @SaCheckPermission("system:countryDict:pageList")
    @GetMapping("/pageList")
    public TableDataInfo<CountryDictVO> pageList(CountryDictDO params, PageQuery pageQuery) {
        return countryDictService.pageList(params, pageQuery);
    }

    /**
     * 查询详细
     */
    @SaCheckPermission("system:countryDict:query")
    @GetMapping(value = "/{id}")
    public R<CountryDictVO> getInfo(@PathVariable Long id) {
        return R.ok(countryDictService.getInfo(id));
    }

    /**
     * 新增
     */
    @SaCheckPermission("system:countryDict:add")
    @PostMapping
    public R<Void> add(@Validated @RequestBody CountryDictDO dto) {
        if (!countryDictService.checkUnique(dto)) {
            return R.fail("新增'" + dto.getCnName() + "'失败，数据已存在");
        }
        countryDictService.add(dto);
        return R.ok();
    }

    /**
     * 修改
     */
    @SaCheckPermission("system:countryDict:edit")
    @PutMapping
    public R<Void> edit(@Validated @RequestBody CountryDictDO dto) {
        if (!countryDictService.checkUnique(dto)) {
            return R.fail("修改'" + dto.getCnName() + "'失败，数据已存在");
        }
        countryDictService.edit(dto);
        return R.ok();
    }

    /**
     * 删除
     */
    @SaCheckPermission("system:countryDict:remove")
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        countryDictService.remove(id);
        return R.ok();
    }
}
